package com.gopay.bis.tbank.mtpay.operation;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.gopay.common.domain.bank.tbank.mtpay.PayResult;
import com.gopay.bis.common.util.XMLBeanUtils;
import com.gopay.common.domain.bank.MtPayBaseMessage;

/**
 * 
 * @MtPayParserMessage.java
 * @author WangHongFei
 */
public class MtPayParserMessage {

    /**
     * 无卡支付解析
     * 
     * @param Object
     * @return MtPayBaseMessage
     */
    public MtPayBaseMessage parseMtPay(Object obj) throws Exception{
        String tempStr = obj.toString();
        MtPayBaseMessage mtPayMessage = new MtPayBaseMessage();
        Map<String, Class> cm = new ConcurrentHashMap<String, Class>();
        cm.put("payResult", PayResult.class);
        PayResult payResult = (PayResult) XMLBeanUtils.xml2Bean(cm, tempStr);
        mtPayMessage = msgFromPayResult(payResult);
        return mtPayMessage;
    }

    private MtPayBaseMessage msgFromPayResult(PayResult payResult) throws Exception{
        MtPayBaseMessage mtPayMessage = new MtPayBaseMessage();
        mtPayMessage.setOrderId(payResult.getOrderNo());
        mtPayMessage.setAmount(new BigDecimal(payResult.getOrderAmount()));
        Map mtbPayHashMap = new ConcurrentHashMap();
        mtbPayHashMap.put("retCd", payResult.getRetCd());
        mtbPayHashMap.put("retMsg", payResult.getRetMsg());
        mtPayMessage.setMtbPayHashMap(mtbPayHashMap);
        return mtPayMessage;
    }

    /**
     * 无卡冲正解析
     * 
     * @param Object
     * @return MtPayBaseMessage
     */
    public MtPayBaseMessage parseMtPayFlush(Object obj) throws Exception{
        String tempStr = obj.toString();
        MtPayBaseMessage mtPayMessage = new MtPayBaseMessage();
        Map<String, Class> cm = new ConcurrentHashMap<String, Class>();
        cm.put("payResult", PayResult.class);
        PayResult payResult = (PayResult) XMLBeanUtils.xml2Bean(cm, tempStr);
        mtPayMessage = msgFromPayResult4Flush(payResult);
        return mtPayMessage;
    }

    private MtPayBaseMessage msgFromPayResult4Flush(PayResult payResult) throws Exception{
        MtPayBaseMessage mtPayMessage = new MtPayBaseMessage();
        mtPayMessage.setOrderId(payResult.getOrderNo());
        Map mtbPayHashMap = new ConcurrentHashMap();
        mtbPayHashMap.put("retCd", payResult.getRetCd());
        mtbPayHashMap.put("retMsg", payResult.getRetMsg());
        mtPayMessage.setMtbPayHashMap(mtbPayHashMap);
        return mtPayMessage;
    }
}
